package com.test.util;

import com.test.bean.BaseConnectionInfo;
import com.test.bean.DataBaseInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @auther Lelouch
 * @create 2018-12-27  9:46
 */
public class CreateBaseConnectionInfo {
    private static Logger logger = LoggerFactory.getLogger(CreateBaseConnectionInfo.class);
    public static BaseConnectionInfo getConnectionInfo(DataBaseInfo baseInfo) {
        if(null != baseInfo) {
            String password = baseInfo.getPassword();
            String userName = baseInfo.getUserName();
            Map<String, String> driverAndUrlMap = getDriverAndUrl(baseInfo);
            String driverName = ConvertUtil.converToString(driverAndUrlMap.get("driverName"));
            String url = ConvertUtil.converToString(driverAndUrlMap.get("url"));
            logger.info("获取的数据库连接信息为：driver：" + driverName + "  url: " + url);
            BaseConnectionInfo connectionInfo = new BaseConnectionInfo();
            connectionInfo.setDriver(driverName);
            connectionInfo.setUrl(url);
            connectionInfo.setName(userName);
            connectionInfo.setPassword(password);
            return connectionInfo;
        }
        return null;
    }

    private static Map<String, String> getDriverAndUrl(DataBaseInfo baseInfo) {
        String dataBaseName = baseInfo.getDataBaseName();
        String dataBaseType = baseInfo.getDataBaseType();
        String port = baseInfo.getPort();
        String hostName = baseInfo.getHostName();
        Map<String, String> result = new HashMap<>();
        String driverName = "";
        String url = "";
        if(null != dataBaseType && !"".equals(dataBaseType)) {
            switch (dataBaseType.toLowerCase()) {
                case "mysql":
                    driverName = "com.mysql.jdbc.Driver";
                    url = "jdbc:mysql://" + hostName + ":" + port + "/" + dataBaseName;
                    break;
                case "mssql":
                    driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                    url = "jdbc:sqlserver://" + hostName + ":" + port + ";databasename=" + dataBaseName;
                    break;
                case "mssqlnative":
                    driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                    url = "jdbc:sqlserver://" + hostName + ":" + port + ";databasename=" + dataBaseName;
                    break;
                case "oracle":
                    driverName = "oracle.jdbc.OracleDriver";
                    url = "jdbc:oracle:thin:@" + hostName + ":" + port + ":" + dataBaseName;
                    break;
                case "postgresql":
                    driverName = "org.postgresql.Driver";
                    url = "jdbc:postgresql://" + hostName + ":" + port + "/" + dataBaseName;
                    break;
                case "db2":
                    driverName = "com.ibm.db2.jdbc.net.DB2Driver";
                    url = "jdbc:db2:" + hostName + ":" + port + "/" + dataBaseName;
                    break;
                case "sysbase":
                    driverName = "com.sybase.jdbc.SybDriver";
                    url = "jdbc:sybase:Tds:" + hostName + ":" + port + "/" + dataBaseName;
                    break;
                default: ;
            }
            result.put("driverName", driverName);
            result.put("url", url);
        }
        return result;
    }
}
